home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Games Collection 1 / software vault.zip / software vault / CDR10 / TGE129C.ZIP / SOURCE / TGEFONT.CPP < prev    next >
C/C++ Source or Header  |  1993-08-20  |  5KB  |  196 lines

  1. /*****************************************************************************
  2. *       The Graphics Engine version 1.29ßC                                   *
  3. *                                                                            *
  4. *       The Graphics Engine code and documentation are Copyright (c) 1993    *
  5. *       by Matthew Hildebrand.                                               *
  6. *                                                                            *
  7. *       Unauthorised usage or modification of any or all of The Graphics     *
  8. *       Engine is strictly prohibited.                                       *
  9. *****************************************************************************/
  10.  
  11. #include <alloc.h>
  12. #include <dir.h>
  13. #include <dos.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include "tgefont.h"
  17. #include "tge.h"
  18.  
  19.  
  20. //*****
  21. //***** Constructor
  22. //*****
  23.  
  24. Font::Font(char *filename, unsigned char fg, unsigned char bg)
  25. {
  26.   unsigned char huge *tempBuf, *in, *out;
  27.   char signature[8];
  28.   unsigned long fileLen;
  29.   unsigned count1, count2;
  30.   FILE *inFile;
  31.   unsigned char byte;
  32.  
  33.   //*** Initialize member variables
  34.   rawData = NULL;
  35.   fgColour = fg;
  36.   bgColour = bg;
  37.  
  38.   //*** Open font file
  39.   if ((inFile=fopen(filename,"rb")) == NULL)
  40.     return;
  41.  
  42.   //*** Parse header
  43.   if (!fread(signature, 8, 1, inFile))        // read in signature
  44.     return;
  45.  
  46.   if (memcmp(signature, "TGEFONT1", 8))        // is signature there?
  47.     return;                    // no, quit
  48.  
  49.   if (!fread(&charWide, 2, 1, inFile))        // read in character width
  50.     return;
  51.   if (!fread(&charDeep, 2, 1, inFile))           // read in character depth
  52.     return;
  53.   charSize = charWide * charDeep;        // precalculate for speed
  54.  
  55.   //*** Ensure format correct (ie., file size is charWide*charDeep+12)
  56.   if (fseek(inFile, 0L, SEEK_END))        // seek to end of file
  57.     return;
  58.   fileLen=ftell(inFile);            // get file length
  59.   if (fseek(inFile, 12L, SEEK_SET))        // seek past header
  60.     return;
  61.  
  62.   fileLen -= 12L;                // remove header length
  63.   if (fileLen != (charSize/8)*256L)           // verify size OK
  64.     return;
  65.  
  66.   //*** Read in character data
  67.   (void far*)tempBuf = farmalloc(fileLen);    // allocate some RAM
  68.   if (tempBuf == NULL)                // was function successful?
  69.     return;                    // no, quit
  70.  
  71.   if (!fread(tempBuf, (unsigned)fileLen, 1, inFile)) // read in font data
  72.   {                        // clean up and quit on error
  73.     farfree(tempBuf);
  74.     return;
  75.   }
  76.   fclose(inFile);                // close the file
  77.  
  78.   //*** Allocate required memory blocks
  79.   rawData = farmalloc((long)charSize * 256L);    // bitmap space
  80.   if (rawData == NULL)                // was function successful?
  81.   {                        // no, clean up and quit
  82.     farfree(tempBuf);
  83.     return;                    // no, quit
  84.   }
  85.  
  86.   image = farmalloc(charSize + 4L);        // allocate the image buffer
  87.   if (image == NULL)                // was function successful?
  88.   {                        // no, clean up and quit
  89.     farfree(rawData);
  90.     farfree(tempBuf);
  91.     return;
  92.   }
  93.   else
  94.   {
  95.     ((unsigned*)image)[0] = charWide;        // fill in dimensions
  96.     ((unsigned*)image)[1] = charDeep;
  97.   }
  98.  
  99.   //*** Convert font data from 1 bit/pixel to 1 byte/pixel
  100.   (void far*)in = tempBuf;            // initialize pointers
  101.   (void far*)out = rawData;
  102.   for (count1=0; count1<fileLen; count1++)    // conversion loop
  103.   {
  104.     byte = *in;                    // get current byte
  105.     *out = (byte>>7) & 1;            // bits --> bytes
  106.     out++;
  107.     *out = (byte>>6) & 1;
  108.     out++;
  109.     *out = (byte>>5) & 1;
  110.     out++;
  111.     *out = (byte>>4) & 1;
  112.     out++;
  113.     *out = (byte>>3) & 1;
  114.     out++;
  115.     *out = (byte>>2) & 1;
  116.     out++;
  117.     *out = (byte>>1) & 1;
  118.     out++;
  119.     *out = (byte>>0) & 1;
  120.     out++;
  121.     in++;                    // update pointer
  122.   }
  123.   farfree(tempBuf);                // free temporary memory
  124.  
  125.   //*** Fill in character address array
  126.   (void far*)out = rawData;            // initialize pointer
  127.   for (count2=0; count2<=255; count2++)        // loop for each character
  128.   {
  129.     addrArray[count2] = out;            // fill in current element
  130.     out += charSize;                      // update address
  131.   }
  132. }
  133.  
  134.  
  135. //*****
  136. //***** Destructor
  137. //*****
  138.  
  139. Font::~Font(void)
  140. {
  141.   if (rawData)                    // was memory allocated?
  142.     farfree(rawData);                // yes, free it
  143.   if (image)                    // same procedure
  144.     farfree(image);
  145. }
  146.  
  147.  
  148. //*****
  149. //***** Compute the width, in pixels, of the given string
  150. //*****
  151.  
  152. unsigned Font::wide(char *str)
  153. {
  154.   return (charWide * strlen(str));
  155. }
  156.  
  157.  
  158. //*****
  159. //***** Write a string at the specified location
  160. //*****
  161.  
  162. void Font::put(int x, int y, char *str)
  163. {
  164.   unsigned count, length;
  165.  
  166.   length = strlen(str);                       // store for speed
  167.   for (count=0; count<length; count++)        // loop for each character
  168.     put(x+count*charWide, y, str[count]);    // put the character
  169. }
  170.  
  171.  
  172. //*****
  173. //***** Write a single character at the specified location
  174. //*****
  175.  
  176. void Font::put(int x, int y, char ch)
  177. {
  178.   register unsigned count;
  179.   unsigned char far *in, *out;
  180.  
  181.   (void far*)in = addrArray[(unsigned char)ch];    // initialize temp pointers
  182.   (void far*)out = image;            // pointer to image buffer
  183.   out += 4;                    // bump past dimension info
  184.  
  185.   for (count=0; count<charSize; count++)     // loop for each pixel
  186.   {
  187.     if (*in == 0)                // is it background colour?
  188.       *out = bgColour;                // yes
  189.     else
  190.       *out = fgColour;                // no, must be foreground
  191.     in++;                    // update pointers
  192.     out++;
  193.   }
  194.  
  195.   putImageInv(x, y, image);              // draw the character
  196. }